package com.teradyne.merlin2.ui.client.gin;

import java.util.logging.Logger;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.teradyne.merlin2.ui.client.AppController;
import com.teradyne.merlin2.ui.client.AppControllerImpl;
import com.teradyne.merlin2.ui.client.activities.OrderListActivity;
import com.teradyne.merlin2.ui.client.activities.OrderMenuActivity;
import com.teradyne.merlin2.ui.client.activities.TopMenuActivity;
import com.teradyne.merlin2.ui.client.activitymappers.BottomActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.CenterActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.LeftSideActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.RightSideActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.TopActivityMapper;
import com.teradyne.merlin2.ui.client.locale.AppLocale;
import com.teradyne.merlin2.ui.client.places.AppPlaceHistoryMapper;
import com.teradyne.merlin2.ui.client.providers.OrderListActivityProvider;
import com.teradyne.merlin2.ui.client.providers.OrderMenuActivityProvider;
import com.teradyne.merlin2.ui.client.providers.TopMenuActivityProvider;
import com.teradyne.merlin2.ui.client.resources.AppResources;
import com.teradyne.merlin2.ui.client.views.AppLayout;
import com.teradyne.merlin2.ui.client.views.AppLayoutImpl;
import com.teradyne.merlin2.ui.client.views.OrderListView;
import com.teradyne.merlin2.ui.client.views.OrderListViewImpl;
import com.teradyne.merlin2.ui.client.views.OrderMenuView;
import com.teradyne.merlin2.ui.client.views.OrderMenuViewImpl;
import com.teradyne.merlin2.ui.client.views.TopMenuView;
import com.teradyne.merlin2.ui.client.views.TopMenuViewImpl;

public class GinModule extends AbstractGinModule {
	public static Logger logger = Logger.getLogger(GinModule.class.getName());

	@Override
	protected void configure() {
		// Views
		bind(AppLayout.class).to(AppLayoutImpl.class).in(Singleton.class);
		bind(TopMenuView.class).to(TopMenuViewImpl.class).in(Singleton.class);
		bind(OrderMenuView.class).to(OrderMenuViewImpl.class).in(Singleton.class);
		bind(OrderListView.class).to(OrderListViewImpl.class).in(
				Singleton.class);

		// Activities
		bind(TopMenuActivity.class).toProvider(TopMenuActivityProvider.class);
		bind(OrderListActivity.class).toProvider(OrderListActivityProvider.class);
		bind(OrderMenuActivity.class).toProvider(OrderMenuActivityProvider.class);
		
		// Places
		bind(PlaceHistoryMapper.class).to(AppPlaceHistoryMapper.class).in(
				Singleton.class);

		// Application EventBus
		bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

		// Application Controller
		bind(AppController.class).to(AppControllerImpl.class).in(
				Singleton.class);

		// Internationalization
		bind(AppLocale.class).in(Singleton.class);

		// Resources
		bind(AppResources.class).in(Singleton.class);
	}

	@Provides
	@Singleton
	@Named("TopRegion")
	public ActivityManager getTopRegionActivityMapper(
			TopActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("BottomRegion")
	public ActivityManager getBottomRegionActivityMapper(
			BottomActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("LeftSideRegion")
	public ActivityManager getLeftSideRegionActivityMapper(
			LeftSideActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("RightSideRegion")
	public ActivityManager getRightSideRegionActivityMapper(
			RightSideActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("CenterContentRegion")
	public ActivityManager getMainTopContentRegionActivityMapper(
			CenterActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@SuppressWarnings("deprecation")
	@Provides
	@Singleton
	public PlaceController getPlaceController(EventBus eventBus) {
		return new PlaceController(eventBus);
	}

	@Provides
	@Singleton
	public PlaceHistoryHandler getHistoryHandler(
			PlaceController placeController, PlaceHistoryMapper historyMapper,
			EventBus eventBus) {
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
				historyMapper);
		return historyHandler;
	}

}
